package com.whw.fy.service.impl;

import com.whw.fy.entity.Message;
import com.whw.fy.entity.Student;
import com.whw.fy.utils.AgeUtil;
import com.whw.fy.utils.GetEveryDate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.whw.fy.entity.Staff;
import com.whw.fy.mapper.StaffMapper;
import com.whw.fy.service.StaffService;

import java.util.*;

@Service
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffMapper staffMapper;

    @Override
    public Message deleteByPrimaryKey(Integer staffId) {
        return staffMapper.deleteByPrimaryKey(staffId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Staff record) {
        return staffMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Staff record) {
        return staffMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer staffId) {
        Staff staff = staffMapper.selectByPrimaryKey(staffId);
        if (staff.getStaffBirth() != null) {
            staff.setStaffAge(AgeUtil.getAgeByBirth(staff.getStaffBirth()));
        } else {
            staff.setStaffAge(-1);
        }
        return Message.success(null).add(staff);
    }

    @Override
    public Message updateByPrimaryKeySelective(Staff record) {
        return staffMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Staff record) {
        return staffMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(initStaffList(staffMapper.selectAll()));
    }

    @Override
    public Message selectAllByJurisdiction(int Jurisdiction) {
        return Message.success(null).add(staffMapper.selectAllByStaffJurisdiction(Jurisdiction));
    }

    @Override
    public Message selectStaffnameAndStaffIdByJurisdiction(int Jurisdiction) {
        List<Staff> staffList = staffMapper.selectAllByStaffJurisdiction(Jurisdiction);
        List<Map> mapList = new LinkedList<>();
        for (Staff staff : staffList) {
            HashMap map = new HashMap();
            map.put("staffId", staff.getStaffId());
            map.put("String", staff.getStaffName() + "<" + staff.getStaffId() + ">");
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message selectStaffnameAndStaffId() {
        List<Staff> staffList = staffMapper.selectAll();
        List<Map> mapList = new LinkedList<>();
        for (Staff staff : staffList) {
            HashMap map = new HashMap();
            map.put("staffId", staff.getStaffId());
            map.put("String", staff.getStaffName() + "<" + staff.getStaffId() + ">");
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }


    @Override
    public int batchInsert(List<Staff> list) {
        return staffMapper.batchInsert(list);
    }

    private List<Staff> initStaffList(List<Staff> staffList) {
        for (Staff staff : staffList) {
            //根据学生出生日期动态设置学生年龄
            if (staff.getStaffBirth() != null) {
                staff.setStaffAge(AgeUtil.getAgeByBirth(staff.getStaffBirth()));
            } else {
                staff.setStaffAge(-1);
            }

        }
        return staffList;
    }

    @Override
    public int updateBatch(List<Staff> list) {
        return staffMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<Staff> list) {
        return staffMapper.updateBatchSelective(list);
    }
}



